package com.sandasai.action.authority;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.sandasai.service.AdminManager;
import com.sandasai.vo.SettingBean;
public class RegistAuthorityInterceptor extends AbstractInterceptor {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private AdminManager adminManager;
    
    public AdminManager getAdminManager() {
        return adminManager;
    }
    public void setAdminManager(AdminManager adminManager) {
        this.adminManager = adminManager;
    }
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        SettingBean settingBean = adminManager.getSettingBean();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            Date startTimeOfRegistration = sdf.parse(settingBean.getStartTimeOfRegistration());
            Date endTimeOfRegistration = sdf.parse(settingBean.getEndTimeOfRegistration());
            Date currentDate = new Date();
            if(currentDate.after(startTimeOfRegistration) && currentDate.before(endTimeOfRegistration)) {
                return invocation.invoke();
            }else {
                ActionContext ctx = ActionContext.getContext();
                ctx.put("message", "注册开始时间：" + settingBean.getStartTimeOfRegistration() + "\n" + "注册截止时间：" 
                        + settingBean.getEndTimeOfRegistration());
                return Action.ERROR;
            }
        }catch(NullPointerException exception) {
            ActionContext ctx = ActionContext.getContext();
            ctx.put("message", "管理员还没有设置注册时间！");
            return Action.ERROR;
        }
    }
}